package by.dvrudenko.avtobaza.webapp.page.createResponses.panel;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.dvrudenko.avtobaza.datamodel.Responses;
import by.dvrudenko.avtobaza.datamodel.Responses_;
import by.dvrudenko.avtobaza.datamodel.enums.UserRole;
import by.dvrudenko.avtobaza.services.ResponsesService;
import by.dvrudenko.avtobaza.webapp.page.AbstractPanel;
import by.dvrudenko.avtobaza.webapp.page.car.panel.MyPagingNavigator;
import by.dvrudenko.avtobaza.webapp.page.createResponses.CreateResponsesPage;
import by.dvrudenko.avtobaza.webapp.utils.security.SecuredLinkForAdminAjax;

public class ResponsesListPanel extends AbstractPanel {

	@Inject
	private ResponsesService responsesService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ResponsesListPanel.class);

	public ResponsesListPanel(String id) {
		super(id);
		setOutputMarkupId(true);
		ResponsesDataProvider responsesDataProvider = new ResponsesDataProvider();

		final WebMarkupContainer tableBody = new WebMarkupContainer("wrapper-body");

		add(tableBody);
		// таблица с выводом отзывов
		DataView<Responses> dataView = new DataView<Responses>("list", responsesDataProvider, 3) {
			@Override
			protected void populateItem(Item<Responses> item) {
				final Responses responses = item.getModelObject();				
				
				item.add(new Label("user.name"));				
				item.add(new Label("responsesDate"));
				item.add(new Label("responsesText"));	
				
					// удалить отзыв
					AjaxLink<Void> removeResponses = new SecuredLinkForAdminAjax("remove-ajax"){
							@Override
							public void onClick(AjaxRequestTarget target) {
								responsesService.delete(responses);
								LOGGER.info(user.getId() + " delete response");
								target.add(ResponsesListPanel.this);
							};
					};
					
				item.add(removeResponses);
				
					}};
				

		tableBody.add(dataView);

		add(new MyPagingNavigator("paging", dataView));
		add(new OrderByBorder<SingularAttribute<Responses, ?>>("sortByResponsesDate", Responses_.responsesDate, responsesDataProvider));
		/*add(new OrderByBorder<SingularAttribute<Responses, ?>>("sortByUserName", Responses_.user, responsesDataProvider));*/
			
	}

	private class ResponsesDataProvider extends SortableDataProvider<Responses, SingularAttribute<Responses, ?>> {

		public ResponsesDataProvider() {
			super();
			setSort(Responses_.responsesDate, SortOrder.ASCENDING);
		}

		@Override
		public Iterator<? extends Responses> iterator(long first, long count) {

			SingularAttribute<Responses, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
			return responsesService.getAllResponses(sortParam, ascending, (int) first, (int) count).iterator();
		}

		@Override
		public long size() {
			return responsesService.getCount();
		}

		@Override
		public IModel<Responses> model(Responses responses) {
			return new CompoundPropertyModel<Responses>(responses);
		}
	}

}
